﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.FileLayer;


namespace HA.ModFlow.Domain.DomainLayer
{
    [Serializable]
    public class UZFCollection : BoundaryCollecionBase
    {

        #region 构造函数

        public UZFCollection(int nrows, int ncolumns, int nlayers, int nPeriods)
            : base(nrows, ncolumns, nlayers)
        {

            this._uzf = new UZF(nrows, ncolumns, nlayers, nPeriods);

        }
        #endregion


        #region 字段属性

        UZF _uzf;

        public UZF Uzf
        {
            get { return _uzf; }
            set { _uzf = value; }
        }






        #endregion



        #region 公共函数

        public override void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
        {
            base._stressPeriods_StressPeriodChanged(sender, e);


            //List<UZFPeriod> periods = e.Param as List<UZFPeriod>;
            List<StressPeriod> periods = e.Param as List<StressPeriod>;

            this.Uzf.OnResetSperiods(periods.Count);

            ///瞬态应力期
            this.Uzf.IsHaveTransient = false;
            foreach (StressPeriod item in periods)
            {
                if (!item.IsSteadyState)
                {
                    this.Uzf.IsHaveTransient = true;
                    return;
                }               
            }
        }



        public void SetUZF(UZF uzf)
        {
            this.Uzf.NUZTOP = uzf.NUZTOP;
            this.Uzf.IUZFOPT = uzf.IUZFOPT;
            this.Uzf.IRUNFLG = uzf.IRUNFLG;
            this.Uzf.IETFLG = uzf.IETFLG;
            this.Uzf.NTRAIL2 = uzf.NTRAIL2;
            this.Uzf.NSETS2 = uzf.NSETS2;
            this.Uzf.SURFDEP = uzf.SURFDEP;

            //和应力期无关的数组数据

            this.Uzf.IUZFBND = (int[,])uzf.IUZFBND.Clone();
            this.Uzf.IRUNBND = (int[,])uzf.IRUNBND.Clone();
            this.Uzf.VKS = (double[,])uzf.VKS.Clone();
            this.Uzf.EPS = (double[,])uzf.EPS.Clone();
            this.Uzf.THIS = (double[,])uzf.THIS.Clone();
            this.Uzf.THTI = (double[,])uzf.THTI.Clone();



            //和应力期相关的数组数据
            int i = 0;
            foreach (UZFPeriod period in uzf.Periods)
            {

                this.Uzf.Periods[i].Multiplier = period.Multiplier;
                this.Uzf.Periods[i].EXTDP = (double[,])period.EXTDP.Clone();
                this.Uzf.Periods[i].EXTWC = (double[,])period.EXTWC.Clone();
                this.Uzf.Periods[i].FINF = (double[,])period.FINF.Clone();
                this.Uzf.Periods[i].PET = (double[,])period.PET.Clone();

                i++;
            }
            this.AskCollectionChanged(new SCColleconChangedEventArgs(uzf, SCCollectionChangedType.MODIFY, SCType.UZF));
        }




        public override void OnGridReset(int nrows, int ncols, int nlayers)
        {
            base.OnGridReset(nrows, ncols, nlayers);

            this._uzf = new UZF(nrows, ncols, nlayers, this.NPeriods);
        }


        public UzfPackage ToUZFPackage()
        {
            UzfPackage uzfP = new UzfPackage();

            uzfP.NUZTOP = this.Uzf.NUZTOP;
            uzfP.IUZFOPT = this.Uzf.IUZFOPT;

            if (this.Uzf.IRUNFLG)
            {
                uzfP.IRUNFLG = 1;
            }
            else
            {
                uzfP.IRUNFLG = 0;
            }

            if (this.Uzf.IETFLG)
            {
                uzfP.IETFLG = 1;
            }
            else
            {
                uzfP.IETFLG = 0;
            }

            //IUZFCB1
            //IUZFCB2

            uzfP.NTRAIL2 = this.Uzf.NTRAIL2;
            uzfP.NSETS2 = this.Uzf.NSETS2;
            uzfP.IsHaveTransient = this.Uzf.IsHaveTransient;
            //NUZGAG


            uzfP.SURFDEP = this.Uzf.SURFDEP;

            uzfP.IUZFBND = (int[,])this.Uzf.IUZFBND.Clone();
            uzfP.IRUNBND = (int[,])this.Uzf.IRUNBND.Clone();
            uzfP.VKS = (double[,])this.Uzf.VKS.Clone();
            uzfP.EPS = (double[,])this.Uzf.EPS.Clone();
            uzfP.THTS = (double[,])this.Uzf.THIS.Clone();
            uzfP.THTI = (double[,])this.Uzf.THTI.Clone();


            //IUZROW
            //IUZCOL
            //IFTUNIT
            //IUZOPT



            //增加一个计数器，强制第一值不允许使用上一次设置

            int i = 1;
            foreach (UZFPeriod item in this.Uzf.Periods)
            {
                StressPeriodData spd = new StressPeriodData();

                if (this._isUsePrecious[i - 1])
                {
                    if (i == 1)
                    {
                        spd.NUZF1 = 1;
                        spd.NUZF2 = 1;
                        spd.NUZF3 = 1;
                        spd.NUZF4 = 1;

                        spd.FINF = item.FINF;
                        spd.PET = item.PET;
                        spd.EXTDP = item.EXTDP;
                        spd.EXTWC = item.EXTWC;
                    }
                    else
                    {
                        spd.NUZF1 = -1;
                        spd.NUZF2 = -1;
                        spd.NUZF3 = -1;
                        spd.NUZF4 = -1;

                        //使用上一周期

                    }
                }
                else
                {
                    spd.NUZF1 = 1;
                    spd.NUZF2 = 1;
                    spd.NUZF3 = 1;
                    spd.NUZF4 = 1;

                    spd.FINF = item.FINF;
                    spd.PET = item.PET;
                    spd.EXTDP = item.EXTDP;
                    spd.EXTWC = item.EXTWC;
                }

                uzfP.StressPeriodDatas.Add(spd);

                i++;
            }

            return uzfP;
        }


        #endregion







    }



}
